<?php

namespace app\common\model;

use think\Model;
use traits\model\SoftDelete;

/**
 * 售后模型
 */
class OrderRefund extends Model
{
    use SoftDelete;

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    protected $deleteTime = 'deletetime';
    // 追加属性
    protected $append = [

    ];


    public function getImagesAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['images']) ? $data['images'] : '');
        if(!$value){ return  [];}
        $value = explode(',',$value);
        foreach ($value as $k=>&$v){
            $v=get_app_image_url($v);
        }
        return $value;
    }
    /**
     * 退款
     * */
    public static function OrderRefundAdd($data){
//        $data;
//        $data = UserInvoice::where(['id'=>$UserInvoice])->find();
//        if(!empty($data)){
//            unset($data['id']);
//            unset($data['createtime']);
//            unset($data['updatetime']);
//            unset($data['apply_status']);
//            unset($data['status']);
//            $data['order_id'] = $order_id;
//            $data['types']    = $type;
//            $data['price']    = $price;
//            return self::create($data);
//        }
//        return  false;

    }


    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function address()
    {
        return $this->belongsTo('order_address', 'address_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function shop()
    {
        return $this->hasOne('shop_apply', 'id', 'order.sell_id', [], 'LEFT')->setEagerlyType(0);
    }




    public function orderGoods()
    {
        return $this->hasMany('OrderGoods', 'order_id', 'id');
    }
    public function order_goods()
    {
        return $this->hasMany('OrderGoods', 'order_id', 'id');
    }
    public function getModeList()
    {
        return ['1' => __('Mode 1'), '2' => __('Mode 2')];
    }


    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4'), '5' => __('Status 5'), '6' => __('Status 6'), '7' => __('Status 7')];
    }

    public function getPaytimeAttr($value, $data)
    {
        $value = $value ? $value : ($data['paytime'] ? $data['paytime'] : '');
        return $value ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getSendtimeAttr($value, $data)
    {
        $value = $value ? $value : ($data['sendtime'] ? $data['sendtime'] : '');
        return $value ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getReceipttimeAttr($value, $data)
    {
        $value = $value ? $value : ($data['receipttime'] ? $data['receipttime'] : '');
        return $value ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCompletetimeAttr($value, $data)
    {
        $value = $value ? $value : ($data['completetime'] ? $data['completetime'] : '');
        return $value ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCanceltimeAttr($value, $data)
    {
        $value = $value ? $value : ($data['canceltime'] ? $data['canceltime'] : '');
        return $value ? date("Y-m-d H:i:s", $value) : $value;
    }
    public function getCreateTimeAttr($value, $data)
    {
        $value = $value ? $value : ($data['createtime'] ? $data['createtime'] : '');
        return $value ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getSamplingTimeAttr($value, $data)
    {
        $value = $value ? $value : ($data['samplingtime'] ? $data['samplingtime'] : '');
        return $value ? date("Y-m-d H:i:s", $value) : $value;
    }
    public function getDetectionTimeAttr($value, $data)
    {
        $value = $value ? $value : ($data['detectiontime'] ? $data['detectiontime'] : '');
        return $value ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getReportTimeAttr($value, $data)
    {
        $value = $value ? $value : ($data['reporttime'] ? $data['reporttime'] : '');
        return $value ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getSenddetectionTimeAttr($value, $data)
    {
        $value = $value ? $value : ($data['senddetectiontime'] ? $data['senddetectiontime'] : '');
        return $value ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPaytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setSendtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setReceipttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCompletetimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCanceltimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function getMaterialAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['material']) ? $data['material'] : '');
        if(!$value){return [];}
        $value=explode(',',$value);
        foreach ($value as $k=>&$v){
            $v=get_app_image_url($v);
        }
        return $value;
    }


    public function getLogoAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['logo']) ? $data['logo'] : '');
        if(!$value){return [];}
//        $value=explode(',',$value);
        $value=get_app_image_url($value);
//        foreach ($value as $k=>&$v){
//            $v=get_app_image_url($v);
//        }
        return $value;
    }
    /**
     * 获取数据
     * */
    public static function getlist($where=[]){

        $list = self::where($where)
            ->alias('a')
            ->field('a.*,o.id as order_id,o.sell_id,s.id,s.company_name,s.logo,o.order_sn')
//            ->with()
            ->order('a.id desc')
            ->join('order o','a.order_id=o.id')
            ->join('shop_apply s','o.sell_id=s.id')
            ->paginate();
//        dd($list);
        foreach ($list as $k=>$v){
            $ordergoods = OrderGoods::where(['order_id'=>$v['order_id']])->select();
            foreach ($ordergoods as $kk=>$vv){
                $vv['thumbnailimage'] = $vv['images'];
            }
            $v['goods']=$ordergoods;
        }
//        foreach ($list as $k=>$v){
////            $v->getRelation('shop')->visible(['company_name','logo','city1','city']);
//            foreach ($v['order_goods'] as $vv){
//                $vv->visible(['goods_name','images','goods_num','goods_price','sku_name','type','goods_id','goods_type']);
//                if($vv['type']==0){
//                    $goods = Goods::where(['id'=>$vv['goods_id']])->find();
//                    $vv['goods_type'] = $goods['type'];
//                }
//            }
//            if(!empty($v['earnest']) && !empty($v['end_price'])){
//                //尾款支付时间
//                if(!$v['end_pricetime']){
//                    $v['is_status']=2;//未支付尾款
//                }
//                //定金支付时间
//                if(!$v['earnesttime']){
//                    $v['is_status']=1;//未支付定金
//                }
//            }
//            $v->shop->city= $v->shop->city1;
//        }
        return $list;
    }

    /**
     * 订单详情
     * */
    public static function getInfo($where){
        $info = self::where($where)
            ->alias('a')
            ->field('a.*,o.id as order_id,o.sell_id,s.id,s.company_name,s.logo,o.order_sn,s.city,s.address,s.username,s.mobile')
            ->order('a.id desc')
            ->join('order o','a.order_id=o.id')
            ->join('shop_apply s','o.sell_id=s.id')
            ->find();

        $ordergoods = OrderGoods::where(['order_id'=>$info['order_id']])->select();
        foreach ($ordergoods as $kk=>$vv){
            $vv['thumbnailimage'] = $vv['images'];
        }
        $info['goods']=$ordergoods;
        return $info;
    }
    /**
     * 开票详情
     * */
    public function order_invoice($order_id){
        $where['order_id'] = $order_id;
        $info = OrderInvoice::where($where)->find();
        return $info;
    }



}
